#!/usr/bin/env ruby

# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is ifx, Interactive Firefox Shell.
#
# The Initial Developer of the Original Code is Kouhei Sutou.
# Portions created by the Initial Developer are Copyright (C) 2010
# the Initial Developer. All Rights Reserved.
#
# Contributor(s): Kouhei Sutou <kou@clear-code.com>
#                 SHIMODA Hiroshi <shimoda@clear-code.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****


require 'readline'
require 'socket'

port = ARGV.shift || 9999

QUIT_MESSAGE = [0x1A].pack("U") + "__QUIT__"

history_file = nil
begin
  history_file = File.expand_path("~/.ifx.history")
rescue ArgumentError
  history_file = "ifx-history.txt"
end
max_history_size = 1000

if defined?(Readline::HISTORY)
  history_file = File.expand_path(history_file)
  if File.exist?(history_file)
    lines = IO.readlines(history_file).collect {|line| line.chomp}
    Readline::HISTORY.push(*lines)
  end

  at_exit do
    lines = Readline::HISTORY.to_a.reverse.uniq.reverse
    lines = lines[-[max_history_size, lines.size].min, max_history_size]
    File::open(history_file, File::WRONLY|File::CREAT|File::TRUNC) do |output|
      output.puts(lines)
    end
  end
end


begin
  TCPSocket.open("localhost", port) do |socket|
    while line = Readline.readline("firefox> ", true)
      begin
        socket.puts(line)
      rescue Errno::EPIPE
        break
      end
      buffer = ""
      while IO.select([socket], [], [], 0.05)
        break if socket.eof?
        buffer << socket.readpartial(4096)
      end
      unless buffer.empty?
        raise Exception.new if buffer[0, 5] == QUIT_MESSAGE
        puts buffer
      end
    end
  end
rescue SystemCallError
  puts "Is UxU server running? (localhost:#{port})"
  exit(1)
rescue Exception
  exit(1)
end
